﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using INTEX.DataBase.Tools;
using System.Data.SQLite;
using System.Collections;
using System.Collections.Generic;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class XSGB09 : PDCBase
    {
        #region=====  vwGB11=======
        CollectionViewSource vwGB11
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwGB11");
            }
        }
        DataRow dwGB11
        {
            get
            {
                if (vwGB11.View == null)
                    return null;
                if ((DataRowView)vwGB11.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwGB11.View.CurrentItem).Row;
            }
        }
        #endregion
        public XSGB09()
        {
            InitializeComponent();
            vm = this.DataContext as MainViewModel;
        } 
        /// <summary>
        /// SetProd 当前订单
        /// SetData保存所有订单，但是用于展示今日订单
        /// SetEnv保存所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                base.Window_Loaded(sender, e);
                if (IsLoaded)
                    return;
                IsLoaded = true;
                if (vm == null)
                {
                    vm = this.DataContext as MainViewModel;
                }
                vm.CPLO = "01";
                vm.CPLB = "CP";
                vm.CPLP = "A       "; 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnConfirmEntry_Click(object sender, RoutedEventArgs e)
        {
            string _date = "", _time = "", _stime = "";
            Helper.GetSTime(ref _date, ref _time, ref _stime);
            string DD = INTEXDBC.GetDBDATE();
            string TT = INTEXDBC.GetDBTIME();
            if (DD == "0") DD = _date;
            if (TT == "0") TT = _time;
            //string GPSPPART = dwGB11["GPSPPART"].ToString();
            var query = from DataRow row in SetProd.ICC_GB1109.AsEnumerable()
                        //where row["GPSPPART"].ToString() == GPSPPART
                        select row;
            foreach (DataRow row in query)
            {
                row["TERMNO"] = vm.TERMNO;
                if (row["ABAID"].ToString().Trim().Length== 0)
                {
                    vm.MAXORD = vm.MAXORD + 1;
                    row["ABAID"] = vm.TERMNO.Trim() + "." + vm.MAXORD;
                }
                row["PROCESSED"] = "N";
                row["JP02"] = "N";                
                row["CPLO"] = vm.CPLO;
                row["CPLB"] = vm.CPLB;
                row["CPLP"] =Helper.Left(vm.CPLP.PadRight(8),8);
                row["NEWDATE"] = DD;
                row["NEWTIME"] = TT;
                row["NEWUSER"] = vm.app.User;
                vm.SQLiteDatabase.InsertSQLiteTable(row);
            } 
            vm.SQLiteDatabase.ExecuteNonQuery("UPDATE ICC_MAXORD SET MAXORD= '" + vm.MAXORD + "'");
            string path = @"C:\IntexReport\Data\YGRCPGB04";
            vm.INTEXBL.AutoConvert(path, "YGRCPGB04");
            SetProd.Clear();
        }       
    }
}
